﻿using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using LdtJudge.DataEngine.Model.XMLConfigModel.OptimzeMode;
using LdtJudge.DataEngine.Model.XMLConfigModel.SameNodeConfigModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.BaseModule.MultiFack
{
    /// <summary>
    /// 内容取交集
    /// </summary>
    public class MultiFactIntersect : MultiFactBase
    {
        /// <summary>
        /// 共同确认的要素信息
        /// </summary>
        public XmlDocument confirmNode = new XmlDocument();

        public CaseCauseModel caseCauseModel;
        /// <summary>
        /// 事实要素内容获取共同认定的
        /// </summary>
        /// <param name="templateNode">模板</param>
        /// <param name="personxml">分人员合并后的集合</param>
        /// <param name="xmlConfigModels">提取认定要素配置</param>
        /// <param name="nodeHandleType">同节点判定类型</param>
        /// <returns></returns>
        public override string MultiFactHandleCommon(XmlNode templateNode, Dictionary<Tuple<string, string>, XmlNode> personxml, List<SameNodeXml> xmlConfigModels, SameNodeHandleType nodeHandleType)
        {
            //新建一个此案由的根节点xml
            XmlDocument result = new XmlDocument();
            string reultxml = string.Format("<{0}></{0}>", templateNode.ParentNode.Name);
            if (caseCauseModel!=null)
            {
                reultxml = string.Format("<{0} 案由名称='{1}' 案由ID='{2}'></{0}>", templateNode.ParentNode.Name,caseCauseModel.CC_Name,caseCauseModel.CC_ID);
            }
            result.LoadXml(reultxml);
            if (personxml.Count>0)
            {
                XmlNode xmlNodeFact = result.CreateElement("事实要素");
                result.DocumentElement.AppendChild(xmlNodeFact);
                var peronkey = personxml.Keys.ToList()[0];
                XmlNode loopNode = personxml[peronkey];
                foreach (var person in personxml.Where(ke => ke.Key != peronkey))
                {
                    xmlNodeFact.RemoveAll();
                    this.CompareNode(templateNode, loopNode, person.Value, xmlConfigModels, xmlNodeFact, nodeHandleType);
                    loopNode = result.Clone();
                }
                //去除空节点
                result = base.EscapeNodeIndexAndBlank(result);
                //将确认的要素赋值
                confirmNode = result;
                dataContext.InteractiveShow.LogShow("\r\n----------★共同认定的事实要素为：★----------\r\n" + result.OuterXml);
            }
           
            if (result.OuterXml == reultxml)
                return string.Empty;
            else
                return result.OuterXml;

        }



        /// <summary>
        /// 进行节点的对比
        /// </summary>
        /// <param name="templateNode">对比模板</param>
        /// <param name="no1">基础节点</param>
        /// <param name="no2">被对比节点</param>
        /// <param name="xmlConfigModels">对比配置</param>
        /// <param name="result">结果值</param>
        /// <param name="nodeHandleType">同节点判定类型</param>
        public void CompareNode(XmlNode templateNode, XmlNode no1, XmlNode no2, List<SameNodeXml> xmlConfigModels, XmlNode result, SameNodeHandleType nodeHandleType)
        {
            #region 针对事实要素的属性进行特殊处理
            if (templateNode.Name == "事实要素")
            {//抽取同案要素
                foreach (XmlAttribute xmlattr in templateNode.Attributes)
                {
                    string xmlpath = base.GetXmlPath(templateNode);
                    XmlNode node1 = no1.SelectSingleNode(xmlpath + "/@" + xmlattr.Name);
                    XmlNode node2 = no2.SelectSingleNode(xmlpath + "/@" + xmlattr.Name);
                    XmlNode nodemerge = node2 == null ? node1 : node2;
                    if (nodemerge != null)
                    {
                        XmlAttribute notemp = (XmlAttribute)result.OwnerDocument.ImportNode(nodemerge, true);
                        result.Attributes.Append(notemp);
                    }
                }
            }
            #endregion

            foreach (XmlNode nodetemplate in templateNode.ChildNodes)
            {
                string xmlpath = base.GetXmlPath(nodetemplate);
                //默认将当前节点路径赋值节点查询路径
                string rootpath = xmlpath;
                if (templateNode.Name != "事实要素")
                {
                    rootpath = xmlpath.Substring(xmlpath.LastIndexOf('/') + 1);
                }
                List<XmlNode> nobase = no1.SelectNodes(rootpath).Cast<XmlNode>().ToList();
                List<XmlNode> nodetwo = no2.SelectNodes(rootpath).Cast<XmlNode>().ToList();
                SameNodeXml xmlConfigModel = xmlConfigModels.FirstOrDefault(con => con.XmlPath == xmlpath);
                if (nobase != null && nobase.Count > 0 && nodetwo != null && nodetwo.Count > 0)
                {  //判定存在当前节点信息
                    for (int i = 0; i < nobase.Count;)
                    {
                        bool nobaseisadd = true;
                        XmlNode no_one = nobase[i];
                        for (int j = 0; j < nodetwo.Count;)
                        {
                            bool nodetwoisadd = true;
                            XmlNode no_two = nodetwo[j];
                            bool issameNode = base.IsSameNode(xmlConfigModel, no_one, no_two, nodeHandleType);
                            if (issameNode)
                            {//如果节点相同

                                XmlNode newnod = null;
                                if (xmlConfigModel != null && xmlConfigModel.OverAll == "1")
                                {
                                    //当前节点为整体处理方式
                                    newnod = result.OwnerDocument.ImportNode(no_one, false);
                                }
                                else
                                {
                                    newnod = result.OwnerDocument.CreateElement(no_one.Name);
                                    foreach (XmlAttribute attr1 in no_one.Attributes)
                                    {
                                        XmlAttribute att2 = no_two.Attributes.Cast<XmlAttribute>().ToList().FirstOrDefault(at2 => at2.Name == attr1.Name);
                                        if (att2 != null && att2.Value == attr1.Value)
                                        {
                                            if (att2.Name.ToLower() == "id")
                                            {
                                                XmlAttribute attributet = (XmlAttribute)newnod.OwnerDocument.ImportNode(att2, true);
                                                attributet.Value = attr1.Value + ","+att2.Value;
                                                newnod.Attributes.Append(attributet);

                                            }
                                            else
                                            {
                                                XmlAttribute attributet = (XmlAttribute)newnod.OwnerDocument.ImportNode(att2, true);
                                                newnod.Attributes.Append(attributet);
                                            }
                                            

                                        }
                                        
                                    }

                                }
                                //XmlNode newnod = result.OwnerDocument.ImportNode(no_one, false);
                                result.AppendChild(newnod);
                                //进行递归获取子节点信息
                                CompareNode(nodetemplate, no_one, no_two, xmlConfigModels, newnod, nodeHandleType);
                                nobase.Remove(no_one);
                                nodetwo.Remove(no_two);
                                nobaseisadd = false;
                                nodetwoisadd = false;
                                break;
                            }
                            if (nodetwoisadd)
                                j++;
                        }
                        if (nobaseisadd)
                            i++;
                    }
                }
            }

        }


    }
}
